<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Model.php');
require_once($Config->WebCMSRoot.'Data/Navigation.php');

Class NavigationModel extends BaseModel
{
	var $Title;
	var $Link;
	var $Subnavigation;
	
	var $ParentID;

	function NavigationModel($title='', $link='')
	{
		$this->Title         = $title;
		$this->Link          = $link;
		$this->Subnavigation = array();
		
		parent::BaseModel();
	}
	
	function Retrieve(&$parameters, $includeSub = true)
	{
		$db = new NavigationData();
		$this->Parameters = &$parameters;
//print('<pre>'.var_export($parameters, true).'</pre>');exit;
		
		//$this->ParentID = '-1';
		//$this->ParentID = ($this->ParameterExists('parentID') ? $this->GetParameter('parentID') : '-1');
		$this->ParentID = $this->GetParameter('parentID');
		$this->ID       = $this->GetParameter('id');
		//$this->Key     = $this->GetParameter('key');
		//$this->Content = $this->GetParameter('content');
		
		$db->Retrieve(&$this);

		if ($includeSub)
		{
			// Loop only 1 lever (for now)
			// Use recursive function ?
			while (list ($key) = each ($this->Collection))
			{
				$subNav = new NavigationModel();
				$subNav->ParentID = $this->Collection[$key]->ID;
				$db->Retrieve(&$subNav);
				$this->Collection[$key]->Subnavigation = &$subNav->Collection;
			}
		}
	}
	
	function RetrieveFlattened(&$parameters)
	{
		$this->Retrieve($this->parameters);

		// flatten navigation
		// Only 2 levels (for now)
		// use recursive function?
		$flatNavigation = array();
		foreach($this->Collection as $key=>$nav)
		{
			$flatNavigation[$nav->ID] = array('ID'=>$nav->ID, 'Title'=>$nav->Title, 'Selected'=>false);
			foreach($nav->Subnavigation as $keySub=>$navSub)
			{
				$flatNavigation[$navSub->ID] = array('ID'=>$navSub->ID, 'Title'=>' -- ' . $navSub->Title, 'Selected'=>false);
			}
		}

		return $flatNavigation;
	}
	
	function AddSubnavigation(&$parent, &$sub)
	{
		$parent->Subnavigation[] = $sub;
	}
}
?>